<?php
/**
 * Created by PhpStorm.
 * User: lg
 * Date: 2017/12/18
 * Time: 18:47
 */

namespace app\master\controller;

use tree\Data;


/**
 * rbac模块
 * Class Rbac
 * @package app\master\controller
 */
class Rbac extends Base
{
    /**
     * 权限列表
     */
    public function index()
    {

        return $this->fetch();
    }

    /**
     * 权限列表
     * @return \think\response\Json
     */
    public function getPermission()
    {


        $data = model('Permission')->getList();
        $list = Data::tree($data, 'permission_name', 'permission_id');
        return $this->_table_return(0, '成功', 0, $data);
    }

    /**
     * 修改菜单
     */
    public function editPermissions()
    {
        $field = input('post.field');
        $value = input('post.value');
        $id = input('post.id');
        if (false === model('permission')->save([$field => $value], ['permission_id' => $id])) {
            $this->_ajax_return(603, '修改失败');
        }
        $this->_ajax_return(200, '修改成功');
    }

    /**
     * 修改菜单弹出层 todo
     */
    public function editPermission()
    {
        $id = input('post.id');
    }

    /**
     * 添加菜单
     */
    public function addpermission()
    {
        $this->view->engine->layout(false);
        if (!request()->isPost()) {
            $id = intval(input('id'));
            $this->assign('id', $id);
            return $this->fetch('addpermission');
        }
        $data['pid'] = intval(input('post.id'));
        $data['permission_name'] = input('post.permission_name');
        $data['rule'] = input('post.rule');
        $data['icon'] = input('post.icon');
        $data['order_number'] = input('post.order_number');
        $data['status'] = input('post.status');
        $data['type'] = input('post.type');

        if (false === model('permission')->addPermission($data)) {
            return $this->_ajax_return(603, '添加失败');
        };
        return $this->_ajax_return(200, '添加成功');
    }

    /**
     * 用户组
     */
    public function role()
    {
        return $this->fetch();
    }

    /**
     * 获取用户组
     */
    public function getRole()
    {
        $list = model('Role')->getList(['status' => 1]);
        return $this->_table_return(0, '成功', 0, $list);
    }

    /**
     * 添加用户组
     * @return mixed|\think\response\Json
     */
    public function addrole()
    {
        $this->view->engine->layout(false);
        if (!request()->isPost()) {
            $id = input('id');
            $this->assign('id', $id);
            return $this->fetch();
        }
        $data = input('post.');
        if (false === db('role')->insert($data)) {
            return $this->_ajax_return(603, '添加失败');
        }
        return $this->_ajax_return(200, '添加成功');
    }

    /**
     * 修改用户组权限
     * @return mixed|\think\response\Json
     */
    public function userrole()
    {
        if (!request()->isPost()) {
            $id = input('id');
            $this->assign('role_id', $id);
            $permission = model('permission')->getList();
            $permission = Data::channelLevel($permission, 0, '&nbsp;', 'permission_id');
            $this->assign('permission', $permission);
            $roleList = db('role_permission')->where(['role_id' => $id])->column('permission_id');
            $this->assign('roleList', $roleList);
            return $this->fetch('userRole');
        }
        $permission = input('post.permissions/a');
        $id = input('id');
        db('role_permission')->where(['role_id' => $id])->delete();
        $d = [];
        foreach ($permission as $v) {
            $d[] = ['role_id' => $id, 'permission_id' => $v];
        }
        db('role_permission')->insertAll($d);
        return $this->_ajax_return(200, '修改成功', url('role'));
    }

    /**
     * 用户列表
     * @return mixed
     */
    public function user()
    {
        return $this->fetch();
    }

    /**
     * 获取用户列表
     */
    public function userList()
    {
        $list = model('Admin')->getUser();
        return $this->_table_return(0, '成功', 0, $list);
    }

    /**
     * 添加用户
     */
    public function addUser()
    {

        if (!request()->isPost()) {
            $this->view->engine->layout(false);
            $role_list = model('role')->getList(['status' => 1]);
            $this->assign('role', $role_list);
//            $warehouse_list = model('Warehouse')->getList(['status' => 1]);
//            $this->assign('warehouse', $warehouse_list);
            return $this->fetch('addUser');
        }

        $admin['username'] = input('username');
        $admin['password'] = md5(input('password'));
        $admin['phone'] = input('phone');
        $admin_info['nick_name'] = input('nickname');
//        $admin_info['warehouse_id'] = input('warehouse_id');
        $role['role_id'] = input('role_id');

        if (db('admin')->where(['status' => 1, 'username' => $admin['username']])->find()) {
            return $this->_ajax_return(603, '用户名重复');
        }
        db()->startTrans();
        try {
            //添加用户
            $admin_id = db('admin')->insertGetId($admin);
            //添加用户基本信息
            $admin_info['admin_id'] = $admin_id;
            db('admin_info')->insertGetId($admin_info);
            //分配权限
            $role['admin_id'] = $admin_id;
            db('admin_role')->insertGetId($role);
        } catch (Exception $e) {
            db()->rollback();
            return $this->_ajax_return(603, '添加失败');
        }
        db()->commit();
        return $this->_ajax_return(200, '添加成功');

//            return $this->fetch('addUser');

    }

    /**
     * 修改用户
     */
    public function edituser()
    {
        $this->view->engine->layout(false);
        if (!request()->isPost()) {
            $id = input('id');
            $user = db('admin')->alias('a')->join('admin_info ai', 'a.admin_id = ai.admin_id')->join('admin_role ar', 'ar.admin_id = a.admin_id')->where(['a.admin_id' => $id])->find();
            $this->assign('user', $user);
            $role_list = model('role')->getList(['status' => 1]);
            $this->assign('role', $role_list);
            return $this->fetch('editUser');
        }
        $admin_id = input('admin_id');
        $username = input('username');
        $phone = input('phone');
        $arr = [
            "username" => $username,
            "phone" => $phone
        ];
        if (input('password')) {
            $arr["password"] = md5(input('password'));
        }
        $role_id = input('role_id');
        db('admin')->where(['admin_id' => $admin_id])->update($arr);
        db('admin_role')->where(['admin_id' => $admin_id])->update(['role_id' => $role_id]);
        return $this->_ajax_return(200, '修改成功');
    }

}